﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Sharp.Portal.Mvc.Bootstrap
{
    public static class DropdownListExtensions
    {
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static DropdownListBuilder DropdownList(this BootstrapFactory factory, string title)
        {
            var ddl = factory.DropdownList();

            ddl.Title = title;

            return ddl;
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static DropdownListBuilder DropdownList(this BootstrapFactory factory)
        {
            return new DropdownListBuilder();
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="factory"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static DropdownListBuilder DropdownListFor<TModel, TProperty>(this BootstrapFactory<TModel> factory, Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.DropdownList(), expression);
        }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DropdownListBuilder DataSource(this DropdownListBuilder factory, string id)
        {
            factory.Id(id);
            return factory;
        }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static DropdownListBuilder DataSource(this DropdownListBuilder factory, IEnumerable dataSource)
        {
            factory.DataSource = dataSource;
            return factory;
        }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static DropdownListBuilder DataSource(this DropdownListBuilder factory, Type dataSource)
        {
            List<Enum> values = Enum.GetValues(dataSource).Cast<Enum>().ToList();

            factory.DataSource = values;
            return factory;
        }

        public static DropdownListBuilder DisplayField(this DropdownListBuilder factory, string displayField)
        {
            factory.DisplayField = displayField;
            return factory;
        }
        public static DropdownListBuilder ValueField(this DropdownListBuilder factory, string valueField)
        {
            factory.ValueField = valueField;
            return factory;
        }
        /// <summary>
        /// 选中值
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static DropdownListBuilder SelectedValue(this DropdownListBuilder factory, object selectedValue)
        {
            factory.SelectedValue = Convert.ToString(selectedValue);
            return factory;
        }

        /// <summary>
        /// 选中值
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static DropdownListBuilder SelectedValue(this DropdownListBuilder factory, IEnumerable selectedValue)
        {
            if (selectedValue == null)
            {
                return factory;
            }
            if (selectedValue is string)
            {
                factory.SelectedValue = selectedValue.ToString();
            }
            else
            {
                List<object> list = new List<object>();

                foreach (var v in selectedValue)
                {
                    list.Add(v);
                }

                factory.SelectedValue = string.Join(",", list.ToArray());
            }
            return factory;
        }

        /// <summary>
        /// 简单型
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static DropdownListBuilder Simple(this DropdownListBuilder factory)
        {
            factory.Simple = true;
            factory.Title = "请选择";
            return factory;
        }
        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static DropdownListBuilder Multiple(this DropdownListBuilder factory)
        {
            factory.Multiple = true;
            factory.Inputtype = "checkbox";
            factory.Attribute("multiple", "multiple", true);
            return factory;
        }
        private static DropdownListBuilder UnobtrusiveValidationAttributes<TModel, TProperty>(this BootstrapFactory<TModel> factory, DropdownListBuilder builder, Expression<Func<TModel, TProperty>> expression)
        {
            var htmlHelper = factory.HtmlHelper;

            var name = ExpressionHelper.GetExpressionText(expression);

            var fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var attributes = htmlHelper.GetUnobtrusiveValidationAttributes(name, modelMetadata);

            builder.Id(fullHtmlFieldName).Name(fullHtmlFieldName);

            var value = htmlHelper.FormatValue(modelMetadata.Model, null);

            builder.SelectedValue(value);

            builder.Title = modelMetadata.DisplayName;
            builder.Description = modelMetadata.Description;


            var property = typeof(TModel).GetProperties().FirstOrDefault(x => x.Name == name);
            if (property != null)
            {
                if (property.GetCustomAttributes(typeof(CustomFieldAttribute), false).FirstOrDefault() is CustomFieldAttribute display)
                {
                    builder.DisplayField = display.DisplayField;
                    builder.ValueField = display.ValueField;
                }
            }
            //foreach (var attribute in attributes)
            //{
            //    builder.Attribute(attribute.Key, attribute.Value.ToString(), true);
            //}

            builder.ValidationAttributes = attributes;

            return builder;
        }


    }
}
